﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using IT_Tracker.ENTIDADES;

namespace IT_Tracker.DADOS
{
    public class PersistenciaBDUsuario
    {
        private string conString = @"Data Source=STO-DSK-CC001\SQLEXPRESS;Initial Catalog=TicketOlosDB;Integrated Security=True; User Id=prvuser; Password=prvuser;";

        public Usuario GetUsuario(int login)
        {

            SqlConnection con = new SqlConnection(conString);
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("spLoginLocaliza", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Login", login);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Usuario us = new Usuario();
                    us.Login = Convert.ToInt32(dr["Login"]);
                    us.Senha = dr["Senha"].ToString();
                    us.Nome = dr["Usuario"].ToString();
                    us.Setor = dr["Setor"].ToString();
                    us.Permissao = dr["Permissao"].ToString();
                    us.Coments = dr["Coments"].ToString();

                    return us;
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }

        public bool CheckLogin(Usuario user)
        {
            SqlConnection con = new SqlConnection(conString);
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("spLoginCheck", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Login", user.Login);
                cmd.Parameters.AddWithValue("@Senha", user.Senha);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Usuario us = new Usuario();
                    us.Login = Convert.ToInt32(dr["Login"]);
                    us.Senha = dr["Senha"].ToString();
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }

        }

        public void incluirUsuario(Usuario user)
        {

            SqlConnection con = new SqlConnection(conString);
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("spLoginADD", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Login", user.Login);
                cmd.Parameters.AddWithValue("@Senha", user.Senha);
                cmd.Parameters.AddWithValue("@Usuario", user.Nome);
                cmd.Parameters.AddWithValue("@Setor", user.Setor);
                cmd.Parameters.AddWithValue("@Permissao", user.Permissao);
                cmd.Parameters.AddWithValue("@Coments", user.Coments);
                cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                throw ex;  // retorna mensagem de erro
            }
            finally
            {
                con.Close();
            }
        }

        public string deletarUsuario(int login)
        {
            SqlConnection con = new SqlConnection(conString);
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("spLoginDel", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Login", login);
                cmd.ExecuteNonQuery();
                return null; // success 
            }
            catch (Exception ex)
            {
                throw ex;  // retorna mensagem de erro
            }
            finally
            {
                con.Close();
            }
        }

        public string atualizarUsuario(Usuario user)
        {

            SqlConnection con = new SqlConnection(conString);
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("spLoginAtualiza", con);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@Login", user.Login);
                cmd.Parameters.AddWithValue("@Senha", user.Senha);
                cmd.Parameters.AddWithValue("@Usuario", user.Nome);
                cmd.Parameters.AddWithValue("@Permissao", user.Permissao);
                cmd.Parameters.AddWithValue("@Setor", user.Setor);
                cmd.Parameters.AddWithValue("@Coments", user.Coments);

                cmd.ExecuteNonQuery();
                return null; // success 
            }
            catch (Exception ex)
            {
                throw ex;  // retorna mensagem de erro
            }
            finally
            {
                con.Close();
            }

        }


    }
}
